import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class HandlingAlerts{
  private WebDriver driver;
  private String baseUrl;
  
  @Before
  public void setUp() throws Exception {
    driver = new FirefoxDriver();
    baseUrl = "http://www.rediff.com/";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @Test
  public void handlingalerts() throws Exception {
    driver.get(baseUrl);
    driver.findElement(By.linkText("Sign in")).click();
    driver.findElement(By.id("btn_login")).click();
    //To get the text on alert box.
    String alerttext = driver.switchTo().alert().getText();
    System.out.println(alerttext);
    //To click on cancel button on alert window
    //driver.switchTo().alert().dismiss();
    driver.switchTo().alert().accept();
    //To transfer  the wd controls from alert to firefox main window
    driver.switchTo().defaultContent();
    Thread.sleep(5000);
  }

  @After
  public void tearDown() throws Exception {
    driver.quit();
    }
}